package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"next-vue-backend/model"
	"next-vue-backend/utils/errmsg"
	"strconv"
)

// AddIdea 新增想法
func AddIdea (c *gin.Context) {
	var data model.Idea
	_ = c.ShouldBindJSON(&data)
	code = model.CreateIdea(&data)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data": data,
		"message":errmsg.GetErrMsg(code),
	})
}

// GetIdeaList 查看想法列表
func GetIdeaList (c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = -1
	}
	data, code, total := model.GetIdeaList(pageSize, pageNum)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data": data,
		"total": total,
		"message": errmsg.GetErrMsg(code),
	})
}

// EditIdea 编辑想法
func EditIdea (c *gin.Context) {
	var data model.Idea
	c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.EditIdea(id, &data)

	c.JSON(http.StatusOK,gin.H{
		"status": code,
		"message": errmsg.GetErrMsg(code),
	})
}

// DeleteIdea 删除想法
func DeleteIdea (c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteIdea(id)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"message": errmsg.GetErrMsg(code),
	})
}